package com.platform.hjt.erp.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hjt.tools.dao.Page;
import com.hjt.tools.dao.PropertyFilter;
import com.platform.hjt.base.costant.StateValue;
import com.platform.hjt.base.exception.BizException;
import com.platform.hjt.base.service.BaseService;
import com.platform.hjt.erp.dao.ErpStockDAO;
import com.platform.hjt.erp.model.ErpStock;

@Service
public class ErpStockService extends BaseService{

	@Resource
	private ErpStockDAO erpStockDAO;
	
	public boolean saveErpStock(ErpStock erpStock, String storageFlowType){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("erpGoodInfo.erpGoodInfoId", erpStock.getErpGoodInfo().getErpGoodInfoId());
		map.put("erpStorage.erpStorageId", erpStock.getErpStorage().getErpStorageId());
		map.put("color", erpStock.getColor());
		ErpStock oldErpStock = erpStockDAO.findUniqueBy(map);
		if(null == oldErpStock){
			erpStockDAO.saveJ(erpStock);
		} else{
			if(storageFlowType.equals(StateValue.InStorage.getKey())){
				oldErpStock.setGoodCount(oldErpStock.getGoodCount() + erpStock.getGoodCount());
			} else if(storageFlowType.equals(StateValue.OutStorage.getKey())){
				oldErpStock.setGoodCount(oldErpStock.getGoodCount() - erpStock.getGoodCount());
			} else{
				return false;
			}
			
			if(oldErpStock.getGoodCount() < 0 ){
				throw new BizException(oldErpStock.getErpGoodInfo().getGoodName() + "库存不够");
			}
			erpStockDAO.updateJ(oldErpStock);
		}
		return  true;
	}
	
	
	public Page<ErpStock> getErpStockList(Page<ErpStock> page, List<PropertyFilter> propertyFilterList){
		return erpStockDAO.findPage(page, propertyFilterList);
	}
	
	public List<ErpStock> getErpStockList(List<PropertyFilter> filters){
		return erpStockDAO.find(filters);
	}
	
	public ErpStock getErpStock(List<PropertyFilter> filters){
		return erpStockDAO.findUniqueBy(filters);
	}
	
	public ErpStock getErpStock(String propertyName, Object value){
		return erpStockDAO.findUniqueBy(propertyName, value);
	}
	
}
